

#import "ChangeVolumeModule.h"
#import "Imports.h"

@implementation ChangeVolumeModule

- CancelChangeVolume:sender
{
    [NXApp stopModal];
	[ChangePanel close];
	return self;
}

- ChangeVolume:sender
{
    [NXApp runModalFor: ChangePanel];
	return self;
}

- OkayChangeVolume:sender
{
    signed char* TheSoundData8= (signed char*) [[TheModuleController CurrentSound:self] data];
	signed short* TheSoundData16= (signed short int*) TheSoundData8;
	int datalength=[[TheModuleController CurrentSound:self] dataSize];
	int samplingrate=[[TheModuleController CurrentSound:self] samplingRate];
	int x;
	int slider=[PercentChangeField intValue];
	
	[NXApp stopModal];
	
	
	if (samplingrate==SND_RATE_CODEC)
		{
		for(x=1;x<=datalength;x++)
			{
			TheSoundData8[x]=TheSoundData8[x] * slider/100.0;
			if (TheSoundData8 [x] >  127) TheSoundData8 [x] =  127;
			if (TheSoundData8 [x] < -128) TheSoundData8 [x] = -128;
			}
		}
	else
		{
		for(x=1;x<=datalength/2;x++)
			{
			TheSoundData16[x]=TheSoundData16[x]*slider/100;
			if (TheSoundData16 [x] >  32767) TheSoundData16 [x] =  32767;
			if (TheSoundData16 [x] < -32768) TheSoundData16 [x] = -32768;
			}
		}
	[ChangePanel close];
	[TheModuleController SoundChanged: [TheModuleController CurrentSound:self]];
	return self;
}


@end
